package com.ln.controller.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;

import com.ln.controller.BaseController;
import com.ln.dto.ParamsMap;
import com.ln.dto.ResultList;
import com.ln.dto.ResultObj;
import com.ln.pojo.impl.UserPOJO;
import com.ln.service.impl.NotesService;
import com.ln.service.impl.UserService;

@Controller
@RequestMapping("space")
public class SpaceController extends BaseController {
	@Autowired
	private NotesService notesService;
	@Autowired
	private UserService userService;

	@RequestMapping("{user_id}")
	public String index(HttpServletRequest request, Model model,
			@PathVariable("user_id") String user_id) {

		return index(request, model, user_id, "notes");
	}

	@RequestMapping("{user_id}/{option}")
	public String index(HttpServletRequest request, Model model,
			@PathVariable("user_id") String user_id,
			@PathVariable("option") String option) {

		return findData(request, model, user_id, option, 0, 10);
	}

	@RequestMapping("{user_id}/{option}/{index}/{size}")
	public String index(HttpServletRequest request, Model model,
			@PathVariable("user_id") String user_id,
			@PathVariable("option") String option,
			@PathVariable("index") String pageIndex,
			@PathVariable("size") String pageSize) {

		if (!user_id.matches("\\d*") || user_id.length() > 10
				|| !pageIndex.matches("\\d*") || !pageSize.matches("\\d*")) {
			return "redirect:/error/notpage";
		}

		int _pageIndex = 0, _pageSize = 0;

		try {
			_pageIndex = Integer.parseInt(pageIndex);
			_pageSize = Integer.parseInt(pageSize);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} finally {

		}

		return findData(request, model, user_id, option, _pageIndex, _pageSize);
	}

	// 这是查询数据
	private String findData(HttpServletRequest request, Model model,
			String user_id, String option, int pageIndex, int pageSize) {
		int userId = Integer.parseInt(user_id);
		UserPOJO LOGIN_USER = (UserPOJO) WebUtils.getSessionAttribute(request,
				"LOGIN_USER");
		if (LOGIN_USER != null && LOGIN_USER.getId() == userId) {
			// 自己的主页空间
			model.addAttribute("USER", LOGIN_USER);
		} else {
			// 别人的主页空间
			ResultObj resultObj = userService.find(userId);
			model.addAttribute("USER", resultObj);
		}
		ResultList resultList = null;

		if (option.equals("notes")) {
			resultList = notesService.find(ParamsMap
					.create(pageIndex, pageSize).add("user_id", userId)
					.add("_class", 1));
		}

		if (resultList != null && !resultList.isEmpty()) {
			model.addAttribute("LIST_OBJ", resultList);
		}

		model.addAttribute("option", option);

		return "space/index";
	}

}
